/**####################################################################################
  CLASS : mydojo.dijix.TitledGrid
  ==================================
  - Extenson of simple grid widget (not OOP way!! may be in furture...need to be cautious since we are dealing with visuals here)
  - Includes a grid title 
  - Includes customizable "Total Recs." indicator string
  ####################################################################################*/
  
dojo.provide("mydojo.dijix.TitledGrid");

dojo.require("mydojo.dijix.SimpleGrid");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.BorderContainer");

dojo.declare("mydojo.dijix.TitledGrid", dijit.layout.ContentPane , {
		
		title			: "",
		gridStruct 		: [	{name: 'First Name' }, 
							{name: 'Last Name' }, 
							{name: 'Address' }, 
							{name: 'This is'}, 
							{name: 'AxGrid'}
						  ],
		customTotalStr  : "",
		icon			: "",
		// Widget references...
		bcWt			: "",
		titleWt			: "",
		gridWt			: "",
		footerWt		: "",
		trWt			: "",
		
		// ======================================================================
		//	Post create widget
		// ----------------------------------------------------------------------
		postCreate: function() {				
			this.inherited(arguments);	
			// create BC
			var rootNode = document.createElement("div");
			this.bcWt = new dijit.layout.BorderContainer( {id:this.id+"_bc" , style:"width:100%;height:100%"} , rootNode );
			this.domNode.appendChild(this.bcWt.domNode);
			
			// create TOP CP						
			this.titleWt = new dijit.layout.ContentPane( {id:this.id+"_top" , style:"background:#42769D;height:18px;overflow:hidden;border-top:2px solid #3D7498" , region:"top"});
			this.bcWt.addChild(this.titleWt);
			
			// create CENTER CP	-- this is actually SimpleGrid					
			this.gridWt = new mydojo.dijix.SimpleGrid( {id:this.id+"_cen" , region:"center" , data:this.data , gridStruct:this.gridStruct });
			this.bcWt.addChild(this.gridWt);			
			
			// create BOTTOM CP
			this.footerWt = new dijit.layout.ContentPane( {id:this.id+"_btt" , style:"background:#BDD5E4;height:20px;border-bottom:1px solid #3D7498" , region:"bottom"});
			this.bcWt.addChild(this.footerWt);
			
			// create title						
			var temp = "&nbsp;&nbsp;<font color='#FFFFFF'><b>" + ((this.title)? this.title : "Simple Grid") + "<b></font>"; 
								
			this.titleWt.setContent( ""+temp+"" );
					
			// create footer
			this.trWt = document.createElement("span");
			this.trWt.id = this.id+"_totRec";
			temp = (this.customTotalStr)? this.customTotalStr : "Total";
			this.footerWt.setContent("<span class='text2'>"+temp+" : </span>");
			this.footerWt.domNode.appendChild(this.trWt);
			
			this.bcWt.startup();
			this.bcWt.layout();	
			
			this.updateTotalRecs();												
		},
		// ======================================================================
		//	Render response from server 
		// ----------------------------------------------------------------------	
		render: function(response,event){
			this.renderGrid(response);
		},		
		// ======================================================================
		//	Clean up this widget
		// ----------------------------------------------------------------------	
		clean: function(){
			console.log("Running DbGrid.clean() function.")
			this.titleWt.destroyDescendants();
			this.titleWt.destroy();
			this.gridWt.clean();
			this.gridWt.destroyDescendants();
			this.gridWt.destroy();
			this.footerWt.destroyDescendants();
			this.footerWt.destroy();
			this.bcWt.destroyDescendants();
			this.bcWt.destroy();
		},				
		// ======================================================================
		//	Re Structure grid
		// ----------------------------------------------------------------------
		reStruct: function(struct){			
			this.gridWt.reStruct( struct );
		},	
		// ======================================================================
		//	Render grid with given data
		// ----------------------------------------------------------------------
		renderGrid: function(data){
			if(data){
				this.gridWt.renderGrid(data);
				this.updateTotalRecs();
			}		
		},
		// ======================================================================
		//	Add row to grid
		// ----------------------------------------------------------------------
		addRow: function(rows){
			this.gridWt.addRow(rows);
		},
		// ======================================================================
		//	Clean the grid data
		// ----------------------------------------------------------------------
		clearGrid: function(){
			this.gridWt.clearGrid();
			this.updateTotalRecs();
		},	
		// ======================================================================
		//	Set Total Records
		// ----------------------------------------------------------------------
		setTotalRecs: function(val){
			this.trWt.innerHTML = val;			
		},
		// ======================================================================
		//	Update Total Records
		// ----------------------------------------------------------------------
		updateTotalRecs: function(){
			this.trWt.innerHTML = this.gridWt.grid.model.count;			
		}
		
});
